﻿using BusinessEntity;
using BusinessEntity.Assets;
using BusinessEntity.JGB;
using BusinessEntity.JGB.BACK;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Nacos.AspNetCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace Assets.API.Controllers
{
    public class CheckController : MvcControllerBase
    {
        private readonly INacosServerManager _serverManager;
        public CheckController(INacosServerManager serverManager) : base(serverManager)
        {
            _serverManager = serverManager;

        }
        #region "收支明细表"
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetSZMXB(string areacode, string kjqj, string datatype = "1")
        {
            try
            {
                if (string.IsNullOrEmpty(kjqj))
                {
                    return Fail("请选择会计期间！");
                }
                kjqj = kjqj.Replace("-", "");
                string orgs = await Getorg();
                //if (orgs.Contains("320508"))
                //{
                    orgs = await GetDepCode();
                //}
                
                

                //if (orgs.Contains("320508"))
                //{
                //    if (orgs.Length == 15)
                //    {
                //        orgs = orgs.Substring(0, orgs.Length - 3);
                //    }
                //}
                BackSZMXB back = new BackSZMXB();
                DataTable cols = await BusinessRule.Check.GetCols("1");
                DataTable data = await BusinessRule.Check.GetSzmxb(areacode, kjqj,orgs,datatype);
                List<Cols> cs = APICommond.DataToEntity.DataTableToEntityList<Cols>(cols) as List<Cols>;
                back.cols = cs;
                List<SZMXBDZ> list= APICommond.DataToEntity.DataTableToEntityList<SZMXBDZ>(data) as List<SZMXBDZ>;
                back.data = list;
                return Success(back);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> SaveSZMXB([FromBody] List<SZMXBDZ> mxb)
        {
            try
            {
                if (mxb != null)
                {
                    foreach (SZMXBDZ m in mxb)
                    {
                        await BusinessRule.Check.InsertSZMXBDZ(m);
                    }
                }
                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        #endregion

        #region "债务债权表"
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetZWZQB(string areacode, string kjqj, string datatype = "1")
        {
            try
            {
                if (string.IsNullOrEmpty(kjqj))
                {
                    return Fail("请选择会计期间！");
                }
                kjqj = kjqj.Replace("-", "");
                string orgs = await Getorg();
                //if (orgs.Contains("320508"))
                //{
                    orgs = await GetDepCode();
                //}

                BackZWZQB back = new BackZWZQB();
                DataTable cols = await BusinessRule.Check.GetCols("2");
                DataTable data = await BusinessRule.Check.GetZWZQB(areacode, kjqj, orgs,datatype);
                List<Cols> cs = APICommond.DataToEntity.DataTableToEntityList<Cols>(cols) as List<Cols>;
                back.cols = cs;
                List<ZWZQBDZ> list = APICommond.DataToEntity.DataTableToEntityList<ZWZQBDZ>(data) as List<ZWZQBDZ>;
                back.data = list;
                return Success(back);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> SaveZWZQB([FromBody] List<ZWZQBDZ> mxb)
        {
            try
            {
                if (mxb != null)
                {
                    foreach (ZWZQBDZ m in mxb)
                    {
                        await BusinessRule.Check.InsertZWZQBDZ(m);
                    }
                }
                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        #endregion

        #region "科目余额表"
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetKMYEB(string areacode, string kjqj, string datatype = "1")
        {
            try
            {
                if (string.IsNullOrEmpty(kjqj))
                {
                    return Fail("请选择会计期间！");
                }
                kjqj = kjqj.Replace("-", "");
                string orgs = await Getorg();
                //if (orgs.Contains("320508"))
                //{
                    orgs = await GetDepCode();
                //}

                BackKMYEB back = new BackKMYEB();
                DataTable cols = await BusinessRule.Check.GetCols("3");
                DataTable data = await BusinessRule.Check.GetKMYEB(areacode, kjqj, orgs,datatype);
                List<Cols> cs = APICommond.DataToEntity.DataTableToEntityList<Cols>(cols) as List<Cols>;
                back.cols = cs;
                List<KMYEBDZ> list = APICommond.DataToEntity.DataTableToEntityList<KMYEBDZ>(data) as List<KMYEBDZ>;
                back.data = list;
                return Success(back);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> SaveKMYEB([FromBody] List<KMYEBDZ> mxb)
        {
            try
            {
                if (mxb != null)
                {
                    foreach (KMYEBDZ m in mxb)
                    {
                        await BusinessRule.Check.InsertKMYEBDZ(m);
                    }
                }
                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        #endregion

        #region "资产负债表"
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetZCFZB(string areacode, string kjqj, string datatype = "1")
        {
            try
            {
                if (string.IsNullOrEmpty(kjqj))
                {
                    return Fail("请选择会计期间！");
                }
                kjqj = kjqj.Replace("-", "");
                string orgs = await Getorg();
                //if (orgs.Contains("320508"))
                //{
                    orgs = await GetDepCode();
                //}

                BackZCFZB back = new BackZCFZB();
                DataTable cols = await BusinessRule.Check.GetCols("4");
                DataTable data = await BusinessRule.Check.GetZCFZB(areacode, kjqj, orgs,datatype);
                List<Cols> cs = APICommond.DataToEntity.DataTableToEntityList<Cols>(cols) as List<Cols>;
                back.cols = cs;
                List<ZCFZBDZ> list = APICommond.DataToEntity.DataTableToEntityList<ZCFZBDZ>(data) as List<ZCFZBDZ>;
                back.data = list;
                return Success(back);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> SaveZCFZB([FromBody] List<ZCFZBDZ> mxb)
        {
            try
            {
                if (mxb != null)
                {
                    foreach (ZCFZBDZ m in mxb)
                    {
                        await BusinessRule.Check.InsertZCFZBDZ(m);
                    }
                }
                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        #endregion

        #region "收益分配表"
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetSYFPB(string areacode, string kjqj, string datatype = "1")
        {
            try
            {
                if (string.IsNullOrEmpty(kjqj))
                {
                    return Fail("请选择会计期间！");
                }
                kjqj = kjqj.Replace("-", "");
                string orgs = await Getorg();
                //if (orgs.Contains("320508"))
                //{
                    orgs = await GetDepCode();
                //}

                BackSYFPB back = new BackSYFPB();
                DataTable cols = await BusinessRule.Check.GetCols("5");
                DataTable data = await BusinessRule.Check.GetSYFPB(areacode, kjqj, orgs,datatype);
                List<Cols> cs = APICommond.DataToEntity.DataTableToEntityList<Cols>(cols) as List<Cols>;
                back.cols = cs;
                List<SYFPBDZ> list = APICommond.DataToEntity.DataTableToEntityList<SYFPBDZ>(data) as List<SYFPBDZ>;
                back.data = list;
                return Success(back);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> SaveSYFPB([FromBody] List<SYFPBDZ> mxb)
        {
            try
            {
                if (mxb != null)
                {
                    foreach (SYFPBDZ m in mxb)
                    {
                        await BusinessRule.Check.InsertSYFPBDZ(m);
                    }
                }
                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        #endregion

        #region "资产负债表财务"
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetZCFZBCW(string areacode, string kjqj, string datatype = "1")
        {
            try
            {
                if (string.IsNullOrEmpty(kjqj))
                {
                    return Fail("请选择会计期间！");
                }
                if (string.IsNullOrEmpty(areacode))
                {
                    return Fail("请选择单位！");
                }
                kjqj = kjqj.Replace("-", "");
                string orgs = await Getorg();

                BackZCFZBCW back = new BackZCFZBCW();
                List<Cols> cs = new List<Cols>();
                Cols c = new Cols();
                c.col = "org";
                c.name = "单位代码";
                cs.Add(c);
                Cols c1 = new Cols();
                c1.col = "orgname";
                c1.name = "单位名称";
                cs.Add(c1);

                Cols c2 = new Cols();
                c2.col = "qm";
                c2.name = "金额";
                cs.Add(c2);

                Cols c3 = new Cols();
                c3.col = "xm";
                c3.name = "项目";
                cs.Add(c3);

                DataTable data = await BusinessRule.Check.GetZCFZBCW(areacode, kjqj,datatype);
               
                List<ZCFZBCWDZ> list = APICommond.DataToEntity.DataTableToEntityList<ZCFZBCWDZ>(data) as List<ZCFZBCWDZ>;
                back.data = list;
                return Success(back);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> SaveZCFZBCW([FromBody] List<ZCFZBCWDZ> mxb)
        {
            try
            {
                if (mxb != null)
                {
                    foreach (ZCFZBCWDZ m in mxb)
                    {
                        await BusinessRule.Check.InsertZCFZBCWDZ(m);
                    }
                }
                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        #endregion

        #region "利润表财务"
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> GetLRBCW(string areacode, string kjqj, string datatype = "1")
        {
            try
            {
                if (string.IsNullOrEmpty(kjqj))
                {
                    return Fail("请选择会计期间！");
                }
                if (string.IsNullOrEmpty(areacode))
                {
                    return Fail("请选择单位！");
                }
                kjqj = kjqj.Replace("-", "");
                string orgs = await Getorg();

                BackLRBCW back = new BackLRBCW();
                List<Cols> cs = new List<Cols>();
                Cols c = new Cols();
                c.col = "org";
                c.name = "单位代码";
                cs.Add(c);
                Cols c1 = new Cols();
                c1.col = "orgname";
                c1.name = "单位名称";
                cs.Add(c1);

                Cols c2 = new Cols();
                c2.col = "bnlj";
                c2.name = "金额";
                cs.Add(c2);

                Cols c3 = new Cols();
                c3.col = "xm";
                c3.name = "项目";
                cs.Add(c3);

                DataTable data = await BusinessRule.Check.GetLRBCW(areacode, kjqj,datatype);

                List<LRBCWDZ> list = APICommond.DataToEntity.DataTableToEntityList<LRBCWDZ>(data) as List<LRBCWDZ>;
                back.data = list;
                return Success(back);
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> SaveLRBCW([FromBody] List<LRBCWDZ> mxb)
        {
            try
            {
                if (mxb != null)
                {
                    foreach (LRBCWDZ m in mxb)
                    {
                        await BusinessRule.Check.InsertLRBCWDZ(m);
                    }
                }
                return Success("成功");
            }
            catch (Exception ex)
            {
                return Fail(ex.Message);
            }
        }

        #endregion
    }
}
